package view;


import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;

import controller.menubar.commands.EditFile;
import controller.menubar.commands.SystemSerachListener;
import controller.menuiconbar.CopyNameListener;
import controller.menuiconbar.RefreshListener;
import controller.menuiconbar.TableChangeListener;
import controller.menuiconbar.UpListener;

import model.GlobalParams;
import net.sf.image4j.codec.ico.ICODecoder;

public class MenuIconBar extends JPanel {	
	
	private static final long serialVersionUID = 1L;
	
	public MenuIconBar(MainFrame f) {	
		
		frame = f;
		
		setLayout(new WrapLayout(FlowLayout.LEFT));	
		
		ico_refresh = readImage("icons/Wcmicons_23.ico");
		ico_table = readImage("icons/Wcmicons_15.ico");
		ico_thumbnail = readImage("icons/Wcmicons_33.ico");
		ico_tree = readImage("icons/Wcmicons_3.ico");	
		ico_all = readImage("icons/Wcmicons_14.ico");
		ico_up = readImage("icons/Wcmicons_19.ico");
		ico_search = readImage("icons/Wcmicons_48.ico");
		ico_copyName = readImage("icons/Wcmicons_11.ico");	
		ico_notepad = readImage("icons/Wcmicons_46.ico");
		
		btn_refresh = new JButton(ico_refresh);		
		btn_table = new JButton(ico_table);
		btn_thumbnail = new JButton(ico_thumbnail);
		btn_tree = new JButton(ico_tree);
		btn_all = new JButton(ico_all);
		btn_up = new JButton(ico_up);		
		btn_search = new JButton(ico_search);
		btn_copyName = new JButton(ico_copyName);
		btn_notepad = new JButton(ico_notepad);
		
		updateNames();
		
		add(btn_refresh);
		add(new JSeparator(SwingConstants.VERTICAL));
		add(btn_table);		
		//add(btn_tree);
		add(new JSeparator(SwingConstants.VERTICAL));
		//add(btn_up);		
		add(new JSeparator(SwingConstants.VERTICAL));
		add(btn_search);
		add(new JSeparator(SwingConstants.VERTICAL));
		add(btn_copyName);
		add(btn_notepad);	
		
		
		btn_table.addActionListener(new TableChangeListener(frame));		
		btn_up.addActionListener(new UpListener(frame));
		btn_copyName.addActionListener(new CopyNameListener(frame));
		btn_refresh.addActionListener(new RefreshListener(frame));
		btn_search.addActionListener(new SystemSerachListener(frame));
		btn_notepad.addActionListener(new EditFile(frame));
	}
	
	
	private ImageIcon readImage(String filename) {
		
		InputStream inIcon = ClassLoader.getSystemResourceAsStream(filename);
		try {
			//BufferedImage imgIcon = ImageIO.read(inIcon);
			java.util.List<BufferedImage> image = ICODecoder.read(inIcon);
			ImageIcon tmp = new ImageIcon(image.get(0));
			return tmp;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;		
	}
	
	public void updateNames(){
		
		str_MenuIcon_refresh = GlobalParams.getInstance().stringCollection.getStr_MenuIcon_refresh();
		str_MenuIcon_table = GlobalParams.getInstance().stringCollection.getStr_MenuIcon_table();
		str_MenuIcon_thumbnail = GlobalParams.getInstance().stringCollection.getStr_MenuIcon_thumbnail();
		str_MenuIcon_tree = GlobalParams.getInstance().stringCollection.getStr_MenuIcon_tree();
		str_MenuIcon_all = GlobalParams.getInstance().stringCollection.getStr_MenuIcon_all();
		str_MenuIcon_up = GlobalParams.getInstance().stringCollection.getStr_MenuIcon_up();
		str_MenuIcon_search = GlobalParams.getInstance().stringCollection.getStr_MenuIcon_search(); 
		str_MenuIcon_copyName = GlobalParams.getInstance().stringCollection.getStr_MenuIcon_copyName();
		str_MenuIcon_notepad = GlobalParams.getInstance().stringCollection.getStr_MenuIcon_notepad();
		
		btn_refresh.setToolTipText(str_MenuIcon_refresh);
		btn_table.setToolTipText(str_MenuIcon_table);
		btn_thumbnail.setToolTipText(str_MenuIcon_thumbnail);
		btn_tree.setToolTipText(str_MenuIcon_tree);
		btn_all.setToolTipText(str_MenuIcon_all);
		btn_up.setToolTipText(str_MenuIcon_up);		
		btn_search.setToolTipText(str_MenuIcon_search);
		btn_copyName.setToolTipText(str_MenuIcon_copyName);
		btn_notepad.setToolTipText(str_MenuIcon_notepad);
		
	}
	
	private MainFrame frame;
	
	private JButton btn_refresh;
	private JButton btn_table;
	private JButton btn_thumbnail;
	private JButton btn_tree;
	private JButton btn_all;	
	private JButton btn_up;
	private JButton btn_search;
	private JButton btn_copyName;
	private JButton btn_notepad;	
	
	private ImageIcon ico_refresh;	
	private ImageIcon ico_table;
	private ImageIcon ico_thumbnail;
	private ImageIcon ico_tree;
	private ImageIcon ico_all;	
	private ImageIcon ico_up;	
	private ImageIcon ico_search;
	private ImageIcon ico_copyName;
	private ImageIcon ico_notepad;
	
	private String str_MenuIcon_refresh;
	private String str_MenuIcon_table;
	private String str_MenuIcon_thumbnail;
	private String str_MenuIcon_tree;
	private String str_MenuIcon_all;	
	private String str_MenuIcon_up;	
	private String str_MenuIcon_search;
	private String str_MenuIcon_copyName;
	private String str_MenuIcon_notepad;
}
